﻿using PdfiumViewer;
using System;
using System.Collections.Generic;
using System.Drawing.Imaging;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PDF2TXT.Start
{
    public class PdfConvertHelper
    {       
    
        public List<string> PdfToImagePdfViewer(string pdfFilePath)
        {
            List<string> result = new List<string>();

            string fullFilePath = Path.GetFullPath(pdfFilePath);
            PdfToPic(pdfFilePath, "D:\\", ImageFormat.Png, ref result);
            return result;
        }

        public List<string> PdfToImagePdfViewer(string pdfFilePath, string outFilePath)
        {
            List<string> result = new List<string>();

            string fullFilePath = Path.GetFullPath(pdfFilePath);
            PdfToPic(pdfFilePath, outFilePath, ImageFormat.Png, ref result);
            return result;
        }

        /// <summary>
        /// <param name="filePath">pdf文件路径</param>
        /// <param name="outFilePath">picture输出文件路径</param>
        /// <param name="image">文件格式：Jpeg （转换后文件格式，如：Jpeg，Png等）</param>
        /// </summary>
        public void PdfToPic(string filePath, string outFilePath, ImageFormat image, ref List<string> list)
        {
            var pdf = PdfDocument.Load(filePath);
            var pdfpage = pdf.PageCount;
            var pagesizes = pdf.PageSizes;
            outFilePath = $"{outFilePath}\\{DateTime.Now.ToString("yyyyMMdd-HHmmss")}";
            for (int i = 1; i <= pdfpage; i++)
            {
                Size size = new Size();
                size.Height = (int)pagesizes[(i - 1)].Height;
                size.Width = (int)pagesizes[(i - 1)].Width;
                //可以把".jpg"写成其他形式 RenderPage(filePath, i, size, picPath);
                string outFile = $"{outFilePath}({i}).{image}";
                RenderPage(filePath, i, size, outFile, image);
                list.Add(outFile);
            }

        }
        /// <summary>
        /// 渲染图片
        /// </summary>
        /// <param name="pdfPath"></param>
        /// <param name="pageNumber"></param>
        /// <param name="size"></param>
        /// <param name="outputPath"></param>
        /// <param name="mat"></param>
        /// <param name="dpi"></param>
        private void RenderPage(string pdfPath, int pageNumber, System.Drawing.Size size, string outputPath, ImageFormat mat, int dpi = 300)
        {
            using (var document = PdfDocument.Load(pdfPath))
            using (var stream = new FileStream(outputPath, FileMode.Create))
            using (var image = GetPageImage(pageNumber, size, document, dpi))
                image.Save(stream, mat);
        }
        /// <summary>
        /// 根据页数转换图片
        /// </summary>
        /// <param name="pageNumber"></param>
        /// <param name="size"></param>
        /// <param name="document"></param>
        /// <param name="dpi"></param>
        /// <returns></returns>
        private static System.Drawing.Image GetPageImage(int pageNumber, Size size, PdfDocument document, int dpi)
        {
            return document.Render(pageNumber - 1, size.Width, size.Height, dpi, dpi, PdfRenderFlags.CorrectFromDpi);
        }        
    }
}
